<?php

class Admin{
	
	const SESSION_PREFIX = 'admin_';
	
	/**
	 * 添加
	 *
	 * @param unknown_type $name
	 * @param unknown_type $parent
	 * @param unknown_type $ord
	 */
	function add($name,$passwd,$data = array()){
		
		$data['name'] 		= $name;
		$data['passwd'] 	= $this->passwd_md5($passwd);
		$data['created']	= full_date();
		$mAdmin = M('Admin');
		//返回自增id
		$id = $mAdmin->add($data);
		
		return $id;
	}
	
	/**
	 * 登录
	 *
	 * @param unknown_type $name
	 * @param unknown_type $passwd
	 * @return unknown
	 */
	function login($name,$passwd){
		
		$mAdmin = M('Admin');
		$id = $mAdmin->where("name = '$name' AND disabled = 0")->getField("id");
				
		//找到id
		if(!is_null($id)){
			//加密后的密码
			$passwd_md5 = $this->passwd_md5($passwd);
			
			$find_passwd = $mAdmin->where("id = $id AND disabled = 0")->getField("passwd");
			
			if($passwd_md5 == $find_passwd){
				//登录成功后
				$this->login_after($id);
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	/**
	 * 登录成功后写入session
	 *
	 * @param unknown_type $id
	 * @return unknown
	 */
	function login_after($id){
		$admin = $this->get_by_id($id);
		if(!is_null($admin)){
			//更新最后登录时间
			$this->update($admin['id'], array(
				'last_time' => full_date(),
				'last_ip'	=> real_ip()
			));
			
			//更新session
			$this->set_admin_session($admin);
		}
	}
	
	/**
	 * 登录成功后，设置管理员session
	 *
	 * @param unknown_type $admin
	 */
	function set_admin_session($admin){
		
		$wirte_in_session = array(
			'id',
			'name',
			'real_name',
			'mobile_phone',
			'role',
			'group_name',
			'group_code'
		);
		foreach ($admin as $k => $v){
			if(in_array($k,$wirte_in_session)){
				//如果值为空NULL，或空字符串，则session写不进去
				session(self::SESSION_PREFIX.$k,$v);
			}
		}
	}
	
	function get_by_id($id){
		$mAdmin = M('Admin');
		return $mAdmin->table('admin a')
		->join('admin_groups ag ON ag.group_id = a.group_id')
		->field('a.*,ag.group_name,ag.group_code')
		->where("a.id = $id")->find();
	}

    function get_by_mobile_phone($mobile_phone){
        $mAdmin = M('Admin');
        return $mAdmin->table('admin a')
            ->join('admin_groups ag ON ag.group_id = a.group_id')
            ->field('a.*,ag.group_name,ag.group_code')
            ->where("a.mobile_phone = '$mobile_phone'")->find();
    }

	/**
	 * 密码加密
	 *
	 * @param unknown_type $passwd
	 * @return unknown
	 */
	function passwd_md5($passwd){
		$salt = C('PASSWD_SALT');
		return md5(md5($passwd.$salt).$salt);
	}
	
	/**
	 * 当前是否是登录状态
	 *
	 * @return unknown
	 */
	function is_logged(){
		//前缀
		$prefix = self::SESSION_PREFIX;
    	return session('?'.$prefix.'id');
	}
	
	/**
	 * 登录管理者的信息
	 *
	 * @return unknown
	 */
	function logged_info(){
		$p = self::SESSION_PREFIX;
		if($this->is_logged()){
			return array(
				'admin_id' 				=> session($p.'id'),
				'admin_name' 			=> session($p.'name'),
				'admin_real_name' 		=> session($p.'real_name'),
				'admin_mobile_phone' 	=> session($p.'mobile_phone'),
				'admin_role' 			=> session($p.'role'),
				'admin_group_name' 			=> session($p.'group_name'),
				'admin_group_code' 			=> session($p.'group_code'),
			);
		}else{
			return null;
		}
	}
	
	/**
	 * 
	 * 已登录的权限分组编号
	 */
	function logged_group_code(){
	
		//判断是否是运维
		$admin_info = $this->logged_info();
		
		if($admin_info){
			return $admin_info['admin_group_code'];
		}
		
		return '';
	}
	
	/**
	 * 
	 * 是否为指定的分组
	 * @param $group_code
	 */
	function is_group_code($group_code){
		$code = $this->logged_group_code();
		
		if($code === $group_code){
			return true;
		}
		return false;
	}
		
	function lists($where = null){
		
		$whereSql = '1=1';
		if(!empty($where)){
			if(isset($where['group_code'])){
				$whereSql .= " AND ag.group_code='{$where['group_code']}'";
			}			
		}
		
		$mAdmin = M('Admin');
		return $mAdmin->table('admin a')
		->join('admin_groups ag ON ag.group_id = a.group_id')
		->where($whereSql)
		->field("a.*,DATE_FORMAT(a.created,'%Y-%m-%d') AS created_fmt,ag.group_name")
        ->select();
	}
	
	/**
	 * 
	 * 刷手
	 */
	function operator_list(){
		
		return $this->lists(array(
			'group_code'=>'operator'
		));
		
	}
	
	function update($id,$upItems){
		$mAdmin = M('Admin');
		return $mAdmin -> where('id='.$id)->save($upItems);
	}
}